package br.edu.ufcg.lsd.wbis.ui.client.ui.loggedin.admin.home.info;

import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo;
import br.edu.ufcg.lsd.wbis.ui.client.beans.UserInfo.UserPermission;
import br.edu.ufcg.lsd.wbis.ui.client.ui.FormManager;
import br.edu.ufcg.lsd.wbis.ui.client.ui.management.InfoManager;

import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.Form;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.AnchorLayoutData;

/**
 * @author Renato Miceli
 */
public class UpdateInfoFormPanel extends FormPanel implements FormManager<UserInfo> {

	private static final String AUTHORIZED_FIELD = "authorized";

	private static final String NAME_FIELD = "name";

	private static final String EMAIL_FIELD = "email";

	private static final String TYPE_FIELD = "type";

	private static final String NINETY_PERCENT = "90%";

	private final boolean levelSet;

	public UpdateInfoFormPanel(InfoManager<UserInfo> infoManager, InfoManager<UserInfo> loggedInfoManager) {

		this.setBaseCls("x-plain");
		this.setLabelWidth(100);
		this.setWidth(300);
		this.setHeight(170);

		final UserInfo currentInfo = infoManager.getInfo();

		final TextField regName = new TextField("Name", NAME_FIELD);
		regName.setValue(currentInfo.getName());
		this.add(regName, new AnchorLayoutData(NINETY_PERCENT));

		final TextField regEmail = new TextField("E-mail", EMAIL_FIELD);
		regEmail.setValue(currentInfo.getEmail());
		regEmail.setRegex(".+@.+\\.[a-z]+");
		regEmail.setRegexText("Invalid email");
		this.add(regEmail, new AnchorLayoutData(NINETY_PERCENT));

		final UserPermission requesterLevel = loggedInfoManager.getInfo().getLevel();
		if (requesterLevel == UserPermission.ADMIN) {
			ComboBox userPermissionBox = new ComboBox("Type2", TYPE_FIELD);
			userPermissionBox.setStore(new PermissionStore());
			userPermissionBox.setDisplayField(PermissionStore.getColumnName());
			userPermissionBox.setTypeAhead(true);
			userPermissionBox.setMode(ComboBox.LOCAL);
			userPermissionBox.setTriggerAction(ComboBox.ALL);
			userPermissionBox.setSelectOnFocus(true);
			UserPermission infoLevel = currentInfo.getLevel();
			userPermissionBox.setValue(PermissionStore.getValue(infoLevel));
			this.add(userPermissionBox, new AnchorLayoutData(NINETY_PERCENT));

			Checkbox authorizedBox = new Checkbox("Authorized", AUTHORIZED_FIELD);  
			authorizedBox.setChecked(currentInfo.isAuthorized());
			authorizedBox.setDisabled(infoLevel == UserPermission.ADMIN);
			this.add(authorizedBox, new AnchorLayoutData(NINETY_PERCENT));
			
			levelSet = true;
		} else {
			levelSet = false;
		}

		regName.focus();
	}

	/**
	 * {@inheritDoc}
	 */
	public UserInfo parseForm() {
		Form form = this.getForm();
		UserInfo userInfo = new UserInfo();
		userInfo.setName(form.findField(NAME_FIELD).getValueAsString());
		userInfo.setEmail(form.findField(EMAIL_FIELD).getValueAsString());
		if(levelSet){
			userInfo.setLevel(PermissionStore.getPermission(form.findField(TYPE_FIELD).getValueAsString()));
			userInfo.setAuthorized(form.findField(AUTHORIZED_FIELD).getValueAsString().equals("true")? true : false);
		}
		
		return userInfo;
	}

}
